package com.example.corraflorestacorra;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class TelaJogo extends SimpleBaseGameActivity{
	
	// ===========================================================
	// Constantes
	// ===========================================================

	private static final int CAMERA_WIDTH = 800;
	private static final int CAMERA_HEIGHT = 480;
	
	// ===========================================================
	// Campos
	// ===========================================================

	private Camera mCamera;

	private BitmapTextureAtlas mPersonagemTexture;
	private TiledTextureRegion mPersonagemTextureRegion;

	private BitmapTextureAtlas mObstaculoTexture;
	private ITextureRegion mObstaculoTextureRegion;

	private BitmapTextureAtlas mCaminhoTexture;
	private ITextureRegion mCaminhoTextureRegion;

	private Scene mScene;

	private PhysicsWorld mPhysicsWorld;

	private Body mPersonagemBody;
	private TiledSprite mPersonagem;

	@Override
	public EngineOptions onCreateEngineOptions() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
	
	}

	@Override
	protected void onCreateResources() {
		this.mPersonagemTexture = new BitmapTextureAtlas(this.getTextureManager(), 37, 47, TextureOptions.BILINEAR);
		this.mPersonagemTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mPersonagemTexture, this, "personagem.png", 0, 0, 5, 2);
		this.mPersonagemTexture.load();

		this.mCaminhoTexture = new BitmapTextureAtlas(this.getTextureManager(), 800, 480, TextureOptions.REPEATING_NEAREST);
		this.mCaminhoTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mCaminhoTexture, this, "roag_bg11.png", 0, 0);
		this.mCaminhoTexture.load();

		this.mObstaculoTexture = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR);
		this.mObstaculoTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mObstaculoTexture, this, "box.png", 0, 0);
		this.mObstaculoTexture.load();
		
	}

	@Override
	protected Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		this.mScene = new Scene();
		this.mScene.setBackground(new Background(0, 0, 0));

		this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1);

		//this.iniciarCaminho();
		//this.iniciarBordas();
		this.iniciarPersonagem();
		//this.iniciarObstaculos();


		this.mScene.registerUpdateHandler(this.mPhysicsWorld);

		return this.mScene;
	}
	
	private void iniciarPersonagem() {
		this.mPersonagem = new TiledSprite(20, 20, 19, 37, this.mPersonagemTextureRegion, this.getVertexBufferObjectManager());
		this.mPersonagem.setCurrentTileIndex(0);

		final FixtureDef carFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
		this.mPersonagemBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.mPersonagem, BodyType.DynamicBody, carFixtureDef);

		this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(this.mPersonagem, this.mPersonagemBody, true, false));

		this.mScene.attachChild(this.mPersonagem);
	}

}
